package com.ruoyi.lm.controller;

import java.util.*;

import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.lm.domain.LmUsersign;
import com.ruoyi.lm.service.ILmUsersignService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.lm.domain.LmActivity;
import com.ruoyi.lm.service.ILmActivityService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 活动Controller
 * 
 * @author ruoyi
 * @date 2020-09-02
 */
@Controller
@RequestMapping("/phone/lm/activity")
public class LmActivityControllerPhone extends BaseController
{

    @Autowired
    private ILmActivityService lmActivityService;
    @Autowired
    private ILmUsersignService lmUsersignService;
    /**
     * 查询活动列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestBody LmActivity lmActivity)
    {
        List<LmActivity> list = lmActivityService.selectLmActivityList(lmActivity);
        return getDataTable(list);
    }

    /**
     * 查询活动详情
     */
    @PostMapping("/listById")
    @ResponseBody
    public AjaxResult listById(String id)
    {
        List<Map> resultList = new ArrayList<>();
        //添加活动详情
        Map temp = new HashMap();
        temp.put("detail",lmActivityService.selectLmActivityById(id));
        //获取活动报名人数
        LmUsersign lmUsersign = new LmUsersign();
        lmUsersign.setSignId(id);
        List<LmUsersign> childList = lmUsersignService.selectLmUsersignList(lmUsersign);
        temp.put("sign",childList.size());
        resultList.add(temp);
        return new AjaxResult(AjaxResult.Type.SUCCESS,"200",resultList);
    }

    /**
     * 导出活动列表
     */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LmActivity lmActivity)
    {
        List<LmActivity> list = lmActivityService.selectLmActivityList(lmActivity);
        ExcelUtil<LmActivity> util = new ExcelUtil<LmActivity>(LmActivity.class);
        return util.exportExcel(list, "activity");
    }

    /**
     * 新增保存活动
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody LmActivity lmActivity)
    {
        //取详情图片
        String[] images =lmActivity.getActivityPimages().split(",");
        lmActivity.setActivityPic(images[0]);
        lmActivity.setCreatePath("1");
        lmActivity.setActivityStatus("0");
        lmActivity.setId(IdWorker.getId());
        return toAjax(lmActivityService.insertLmActivity(lmActivity));
    }

    /**
     * 修改保存活动
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody LmActivity lmActivity)
    {
        return toAjax(lmActivityService.updateLmActivity(lmActivity));
    }

    /**
     * 审核活动
     */
    @PostMapping("/reviewActivity")
    @ResponseBody
    public AjaxResult reviewActivity(String id)
    {
        LmActivity lmActivity = new LmActivity();
        lmActivity.setId(id);
        lmActivity.setActivityStatus("1");
        lmActivity.setReviewId(ShiroUtils.getSysUser().getLoginName());
        lmActivity.setReviewTime(new Date());
        return toAjax(lmActivityService.updateLmActivity(lmActivity));
    }

    /**
     * 中止活动
     */
    @PostMapping("/stopActivity")
    @ResponseBody
    public AjaxResult stopActivity(String id)
    {
        LmActivity lmActivity = new LmActivity();
        lmActivity.setId(id);
        lmActivity.setActivityStatus("3");
        return toAjax(lmActivityService.updateLmActivity(lmActivity));
    }


    /**
     * 删除活动
     */
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(lmActivityService.deleteLmActivityByIds(ids));
    }
}
